<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /*
    [3, 1, 2, 4]从第一个开始往右遍历,遇到小的就替换
    [3, 1, 1, 1]
    [0, 1, 1, 1]从第二个开始
    [0, 0, 2, 2]第三
    [0, 0, 0, 4]第四
    遍历出来每个数组的和就是结果；
    以第一个数组为例，实际对应的子数组是[3],[3,1],[3,1,2],[3,1,2,4]
    第二个数组是从第二位开始，对应的是[1],[1,2],[1,2,4]
    */
    var sumSubarrayMins = function (arr) {
      let len = arr.length
      let mod = 10 ** 9 + 7
      let ans = 0
      for (let i = 0; i < len; i++) {
        let tempCount = 0
        let min = arr[i]
        for (let j = i; j < len; j++) {
          if (arr[j] <= min) {
            min = arr[j]
          }
          tempCount += min
        }
        ans += tempCount
      }
      return ans % mod
    };
    console.log(sumSubarrayMins(arr = [3, 1, 2, 4]))
  </script>
</body>

</html>